﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AutoTweet.Utilities
{
    public class Common
    {
        /// <summary>
        /// Parse proxy list from txt file
        /// address:port:username:password
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<InternetProxy> GetInternetProxyListFromFile(string filePath)
        {
            List<InternetProxy> result = new List<InternetProxy>();
            StreamReader reader = null;
            string line;

            try
            {
                if (File.Exists(filePath))
                {
                    reader = new StreamReader(filePath);
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] parts = line.Split(new char[] { ':' });
                        InternetProxy proxy = new InternetProxy();
                        proxy.Address = parts[0] + ":" + parts[1];
                        proxy.UserName = parts[2];
                        proxy.Password = parts[3];

                        result.Add(proxy);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Common::GetProxyList", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }

            return result;
        }
    }
}
